var valEmail = false;
var valPSW = false;
var valName = false;
var valCareer = false;

function onload_register() {
	// initial Birthday Date Picker
	$('#regBDate').datepicker({
		inline : true,
		maxDate : '-1d',
		minDate : '01/01/1920',
		defaultDate : '01/01/1980',
		changeMonth : true,
		changeYear : true
	});

	// initial Capcha image
	$("#tdJcapcha")
			.append(
					"<img style=\"cursor:pointer;border:groove\" src=\"jcaptcha.jpg?ver="
							+ new Date().getMilliseconds()
							+ "\" id=\"imgCapcha\" onClick=\"onclick_imgCapcha(this);\" />");
}

// to get a string of HTML indicating information in the HTML as a parameter
// input use RED Characters
function getHtmlInforRed(infor, idDiv) {
	return "<div style=\"background-color:#CCC;color:#F00\" align=\"center\" id=\""
			+ idDiv
			+ "\" name=\""
			+ idDiv
			+ "\">"
			+ "<strong>"
			+ infor
			+ "</strong>" + "</div>";
}

// to get a string of HTML indicating information in the HTML as a parameter
// input use RED Characters
function getHtmlInforGreen(infor, idDiv) {
	return "<div style=\"background-color:#CCC;color:#060\" align=\"center\" id=\""
			+ idDiv
			+ "\" name=\""
			+ idDiv
			+ "\">"
			+ "<strong>"
			+ infor
			+ "</strong>" + "</div>";
}

// to valid if it is a email
function isEmail(strEmail) {
	if (strEmail
			.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true;
	} else {
		return false;
	}
}

function onFocus_regEmail() {
	if (document.getElementById("chkRegEmail").hasChildNodes()) {
		$("#chkRegEmailInfor").remove();
	}
	$("#chkRegEmail").append(
			getHtmlInforRed("Email is used as the evidence to logon.",
					"chkRegEmailInfor"));
}

function onBlur_regEmail() {
	if (document.getElementById("chkRegEmail").hasChildNodes()) {
		$("#chkRegEmailInfor").remove();
	}
	if (isEmail($("#regEmail").val())) {
		sendEMToServer($("#regEmail").val());
	} else {
		$("#chkRegEmail").append(
				getHtmlInforRed("Email INVALID.", "chkRegEmailInfor"));
		valEmail = false;
	}
}

function onBlur_regPSW1() {
	if ($("#regPSW2").val() == "") {
		return;
	} else {
		checkPSWs();
	}
}

function checkPSWs() {
	if (document.getElementById("chkRegPSW1").hasChildNodes()) {
		$("#chkRegPSW1Infor").remove();
	}
	if ($("#regPSW1").val() != $("#regPSW2").val()) {
		$("#chkRegPSW1").append(
				getHtmlInforRed("These 2 Passwords don't match!",
						"chkRegPSW1Infor"));
		valPSW = false;
	} else {
		$("#chkRegPSW1").append(
				getHtmlInforGreen("CHECKED", "chkRegPSW1Infor"));
		valPSW = true;
	}
}

function onBlur_regPSW2() {
	if ($("#regPSW1").val() == "") {
		return;
	} else {
		checkPSWs();
	}
}

function onFocus_regName() {
	if (document.getElementById("chkRegName").hasChildNodes()) {
		$("#chkRegNameInfor").remove();
	}
	$("#chkRegName").append(
			getHtmlInforRed("User Name as the evidence to logon.",
					"chkRegNameInfor"));
}

function onBlur_regName() {
	if (document.getElementById("chkRegName").hasChildNodes()) {
		$("#chkRegNameInfor").remove();
	}

	var username = $("#regName").val();
	if (username == "") {
		$("#chkRegName")
				.append(
						getHtmlInforRed("Please input a User Name.",
								"chkRegNameInfor"));
		valName = false;
	} else {
		sendUNToServer(username);
	}
}

function onBlur_regCareer() {
	if ($("#regCareer").val() == "Nothing") {
		if (document.getElementById("chkRegCareer").hasChildNodes()) {
			$("#chkRegCareerInfor").remove();
		}
		$("#chkRegCareer").append(
				getHtmlInforRed("Please choose your career",
						"chkRegCareerInfor"));
		valCareer = false;
	} else {
		if (document.getElementById("chkRegCareer").hasChildNodes()) {
			$("#chkRegCareerInfor").remove();
		}
		$("#chkRegCareer").append(
				getHtmlInforGreen("CHECKED", "chkRegCareerInfor"));
		valCareer = true;
	}
}

function sendUNToServer(username) {
	var UN = {
		"username" : username
	};

	$.ajax({
		type : "POST",
		url : "CheckUsernameUnique",
		data : UN,
		success : callbackUN,
		dataType : "json"
	});
}

function callbackUN(data) {
	if (data.result) {
		$("#chkRegName").append(
				getHtmlInforGreen("CHECKED", "chkRegNameInfor"));
		valName = true;
	} else {
		$("#chkRegName").append(
				getHtmlInforRed("User Name has already been taken.",
						"chkRegNameInfor"));
		valName = false;
	}
}

function sendEMToServer(email) {
	var EM = {
		"email" : email
	};

	$.ajax({
		type : "POST",
		url : "CheckEmailUnique",
		data : EM,
		success : callbackEM,
		dataType : "json"
	});
}

function callbackEM(data) {
	if (data.result) {
		$("#chkRegEmail").append(
				getHtmlInforGreen("CHECKED", "chkRegEmailInfor"));
		valEmail = true;
	} else {
		$("#chkRegEmail").append(
				getHtmlInforRed("Email has already been taken.",
						"chkRegEmailInfor"));
		valName = false;
	}
}

function sendCCToServer() {
	// alert("Capcha");
	var CC = {
		"jcaptcha" : $("#jcaptcha").val()
	};

	$.ajax({
		type : "POST",
		url : "CheckCapcha",
		data : CC,
		success : callbackCC,
		dataType : "json"
	});
	// alert("post fini");
}

function callbackCC(data) {
	if (data.result) {
		if (document.getElementById("chkRegCapcha").hasChildNodes()) {
			$("#chkRegCapchaInfor").remove();
		}
		$("#chkRegCapcha").append(
				getHtmlInforGreen("CHECKED", "chkRegCapchaInfor"));
		sendPIToServer();
	} else {
		if (document.getElementById("chkRegCapcha").hasChildNodes()) {
			$("#chkRegCapchaInfor").remove();
		}
		$("#chkRegCapcha").append(
				getHtmlInforRed("Capcha INVALID", "chkRegCapchaInfor"));
		onclick_imgCapcha($("#imgCapcha"));
	}
}

function sendPIToServer() {
	alert("Ready to send information to server.");
	var email = $("#regEmail").val();
	var username = $("#regName").val();
	var psw = $("#regPSW1").val();
	var sex=$("input[name='regSex']:checked").val();
	var career = $("#regCareer").val();
	var bday = $("#regBDate").val();
	var isInternal=$("#regInternalAccount").attr("checked");
	
	var PI={
			"email":email,
			"username" : username,
			"psw":psw,
			"sex":sex,
			"career":career,
			"bday":bday,
			"isInternal":isInternal
	};
	
	alert("Here is the information of yours:" +
			"\nEmail:\t\t"+email+
			"\nUsername:\t"+username+
			"\nSex:\t\t"+sex+
			"\nCareer:\t\t"+career+
			"\nBirthday:\t"+bday+
			"\nAsk for a internal"+isInternal
			);
	
	$.ajax({
		type : "POST",
		url : "StorePIDB",
		data : PI,
		success : callbackPI,
		dataType : "json"
	});
	//w@123.com+d+12+undefined+Engineer+10/08/1989+on
}

function callbackPI(data) {
	if (data.result) {
		alert("Congratulations!\nThe new account is created!");
		window.location.href = "Authorised/WelcomePage.jsp";
	} else {
		window.location.href = "Authorised/WrongPage.jsp";
	}
}

function onclick_imgCapcha(imgObj) {
	$(imgObj).fadeOut(
			'fast',
			function() {
				var datenow = new Date();
				$(this).attr('src',
						'jcaptcha.jpg?ver=' + datenow.getMilliseconds()).delay(
						200).fadeIn('slow');
			});
}

function onclick_submit() {
	if (valEmail && valPSW && valName && valCareer) {
		sendCCToServer();
	} else {
		alert("You still have some field not finished\nPlease complete them first!");
	}
}